package com.qm.web;

import java.util.ArrayList;

import javax.servlet.http.HttpSession;

import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qm.model.Users;
import com.qm.service.CommonService;
import com.qm.service.UserService;

@Controller
public class CommonController {
	
	@Autowired
	private UserService uservice;
	@Autowired 
	private CommonService cservice;
	/**
	 * 登录
	 * @param uname
	 * @param upass
	 * @param session
	 * @return 前台页面
	 */
	@RequestMapping(value="/login")
	public String login(@RequestParam("unumber") String unumber, @RequestParam("upassword") String upassword,HttpSession session){
		Users userList = cservice.checkLogin(unumber, upassword);
		if(userList==null){
			return "redirect:../";//如果返回值为空，返回登录页面
		}
		Users user=userList;
		session.setAttribute("user", user);
		if(user.getUtype()==1){//如果返回值是普通用户，则跳转到index
			System.out.println("qiantai");
			return "redirect:user/index";
		}
		else{
			return "redirect:admin/adminInformation";
		}
	}
	/**
	 * 退出登录
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/logout")
	public String logout(HttpSession session)
	{
		session.removeAttribute("user"); 
		session.invalidate();
		return "/index";//如果返回值为空，返回登录页面
	}
	/**	
	 * 修改密码
	 * @param oldPassWord
	 * @return -1 old pass wrong
	 */
	@ResponseBody
	@RequestMapping(value="/changePassword")
	public int changePassWord(String newPassword ,String oldPassword ,HttpSession session) {
		System.out.println(oldPassword);
		System.out.println(newPassword);
		Users u=(Users)session.getAttribute("user");
		if(!u.getUpassword().equals(oldPassword)){
			return -1;
		}
		u.setUpassword(newPassword);
		cservice.changePassword(u);
		session.setAttribute("user", u);
		return 0;
	}
	/**
	 * 修改用户信息
	 * @param uname
	 * @param ugender
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/changeUserInfo")
	public int changeUserInfo(String uname,String ugender,@RequestParam( required=false) String ugrade,HttpSession session){
		int changeResult=0;
		Users u=(Users)session.getAttribute("user");
		u.setUgender(ugender);
		u.setUname(uname);
		int serviceResult=cservice.changeUserInfo(u);
		session.setAttribute("user", u);
		if(serviceResult==1){
		  changeResult=1;
		}
	
			
		return  changeResult;
		
		
	}
	/**
	 * 显示学生信息
	 * @return
	 */
	@RequestMapping("/index")
	public String showLogin(HttpSession session){
		Users u=(Users)session.getAttribute("user");
		if(u==null){
			return "/index";//视图名
		}
		if(u.getUtype()==1){
			return "redirect:user/index";//重定向到另一个URL了
		}
		return "redirect:admin/adminInformation";
	}
	
}
